<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/16
 * Time: 21:00
 */
namespace Pay\Controller;

use Common\Common\Api\Order;
use Common\Common\Api\Ordermember;
use Common\Common\Api\Redis\Channel;
use Common\Common\Api\Redis\Product;

/**
 * Class ManyiController
 * @package Pay\Controller
 * 686
 */
class LiubaliuController extends PayController{
    protected $config;

    public function pay($array){
        $this->config = Channel::getInstance()->getById($array['channel']);
        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>'029']);
        }

        //$product = M("Product")->where(["id"=> I("request.pay_bankcode","")])->find();
        $product = Product::getInstance()->getById(I("request.pay_bankcode",0,"intval"));
        if(!$product){
            $this->showmessage('未找到支付产品!',['code'=>'029']);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $return = $this->orderadd( $parameter );


        $data = array();
        $data['notifyUrl'] = $return['notifyurl'];
        $data['outOrderNo'] = $return['orderid'];
        $data['goodsClauses'] = 'watch';
        $data['tradeAmount'] = number_format($return['amount'],2,'.','');
        $data['code'] = $this->config['mch_id'];

        switch($this->config['paytype']){
            //微信扫码
            case 1:
                $data['payCode'] = 'wxpay';
                break;
            //支付宝
            case 3:
                $data['payCode'] = 'alipay';
                break;
            //支付宝H5
            case 4:
                $data['payCode'] = 'alipay';
                break;
            default:
                $this->showmessage('不支持此产品!',['code'=>'029']);
        }

        ksort($data);
        $str = httpstr($data)."&key=".$this->config['signkey'];

        $data['sign'] = md5($str);

        $apiurl = $this->config['gateway'];
        $res = curl($apiurl, httpstr($data));

        //echo '<pre/>'; print_r($data);
        //echo "signstr: ". $str."<br/><br/>";
        //echo 'apiurl: '.$this->config['gateway'].'<br/><br/>';

        //echo '<pre/>返回参数： '; print_r($res); exit;

        if(!$res){
            $this->showmessage('获取数据失败!',['code'=>'029']);
        }

        $res = json_decode($res, true);
        if(empty($res)){
            $this->showmessage('获取的数据格式不正确!',['code'=>'029']);
        }

        $code = $res['url'];
        if(!$code){
            if($res['message']){
                $this->showmessage($res['message'],['code'=>'029']);
            }

            $this->showmessage('获取二维码失败!',['code'=>'029']);
        }


        $paytype = transArr( C("PAYTYPES"), "id");
        $this->assign("title", $paytype[$this->config['paytype']]);
        $this->assign("return", $return);
        $this->assign("imgurl", C("website.DOMAIN")."/Pay_Qrcode_getCode?&data=".urlencode($code));
        $this->display("WeiXin/Pay");
    }

    //通知
    public function notifyurl(){
        //file_put_contents("a.txt",var_export($_POST,true)."\r\n\r\n");
        //file_put_contents("a.txt",var_export($_GET,true)."\r\n\r\n", FILE_APPEND);
        //file_put_contents("a.txt", file_get_contents('php://input')."\r\n\r\n", FILE_APPEND);
        //file_put_contents("a.txt", date("Y-m-d H:i:s",time())."\r\n\r\n", FILE_APPEND);

        $p = file_get_contents('php://input');

        //$p = '{"shopCode":"686cz1383","outOrderNo":"201809172252207946424","goodsClauses":"watch","tradeAmount":"10.00","code":"0","nonStr":"aT6a1i0Q3mMPw1QB","msg":"SUCCESS","sign":"21ea9a9080973ef642519ddfca88b36b"}';

        $p = json_decode($p, true);
        /*
Array
(
    [shopCode] => 686cz1383
    [outOrderNo] => 201809172252207946424
    [goodsClauses] => watch
    [tradeAmount] => 10.00
    [code] => 0
    [nonStr] => aT6a1i0Q3mMPw1QB
    [msg] => SUCCESS
    [sign] => 21ea9a9080973ef642519ddfca88b36b
)
        );*/

        if(empty($p)){
            exit("no_data");
        }

        $sign = $p['sign'];

        //获取订单信息
        $orderid = $p['outOrderNo'];
        $find = $this->checkOrderNotify($orderid);
        if(!$find){
            echo 'order_not_found'; exit;
        }

        //保存回调数据
     /*   M("Order")->where(array("pay_orderid"=>$orderid))->save(array(

        ));*/
      /*  Order::getInstance()->edit($find['id'],array(
            "server_return"=>file_get_contents('php://input')
            )
        );*/
   /*     Ordermember::getInstance(getIdByBianHao($find['pay_memberid']))->edit($find['id'],array(


        ));*/

        //获取渠道信息
        $this->config = Channel::getInstance()->find(['code'=>'Liubaliu']);
        if(!$this->config){
            $this->config = M("Channel")->where(array('code'=>'Liubaliu'))->find();
        }

        if(!$this->config){
            exit("get_tongdao_fail");
        }

        if(intval($p['code']) != 0){
            exit("pay_fail");
        }

        unset($p['sign']);
        ksort($p);

        $str = httpstr($p)."&key=".$this->config['signkey'];

        $sign2 = md5($str);

        //echo 'sign: '.$sign.'<br/>';
        //echo 'sign2:'.$sign2.'<br/>';
        if($sign != $sign2){
            echo 'sign_wrong';exit;
        }

        //处理支付
        $this->EditMoney($find['pay_orderid'],"Liubaliu",0);

        //回调成功
        echo 'SUCCESS'; exit;
    }

    public function callBackUrl(){
        echo '处理成功'; exit;
    }

    public function getBankName($code){
        $ret = '';

        //农业银行 ABC 建设银行 CCB 兴业银行 CIB 招商银行 CMB 民生银行 CMBC 华夏银行 HXB
        //工商银行 ICBC 上海银行 BOSH 中国银行 BOC 交通银行 COMM 上海浦东发展银行 SPDB 广东发展银行 GDB 平安银行 PINGAN 邮政储蓄 PSBC 上海农村商业银行 SHRCB 北京银行 BOB 广州商业银行 GZCB 光大银行 CEB 温州银行 BOWZ 中信银行 CNCB
        switch($code){
            //北京银行
            case 'BOB':
                $ret = 'BOB';
                break;
            //东亚银行
            case 'BEA':
                $ret = '';
                break;
            //中国工商银行
            case 'ICBC':
                $ret = 'ICBC';
                break;
            //光大银行
            case 'CEB':
                $ret='CEB';
                break;
            //广发银行
            case 'CGB':
                $ret='GDB';
                break;
            //华夏银行
            case 'HXB':
                $ret='HXB';
                break;
            //中国建设银行
            case 'CCB':
                $ret='CCB';
                break;
            //交通银行
            case 'BOCOM':
                $ret='COMM';
                break;
            //中国民生银行
            case 'CMBC':
                $ret='CMBC';
                break;
            //南京银行
            case 'NJCB':
                $ret='';
                break;
            //宁波银行
            case 'NBCB':
                $ret='';
                break;
            //中国农业银行
            case 'ABC':
                $ret='ABC';
                break;
            //平安银行
            case 'PAB':
                $ret='PINGAN';
                break;
            //上海银行
            case 'SHB':
                $ret='';
                break;
            //上海浦东发展银行
            case 'SPDB':
                $ret='SPDB';
                break;
            //深圳发展银行
            case 'SDB':
                $ret='';
                break;
            //兴业银行
            case 'CIB':
                $ret='CIB';
                break;
            //中国邮政储蓄银行
            case 'PSBS':
                $ret='PSBC';
                break;
            //招商银行
            case 'CMB':
                $ret='CMB';
                break;
            //浙商银行
            case 'CZB':
                $ret='';
                break;
            //中国银行
            case 'BOC':
                $ret='BOC';
                break;
            //中信银行
            case 'CTTIC':
                $ret='CNCB';
                break;
            default:
                $this->showmessage('暂不支持此银行卡!',['code'=>'024']);
        }
        if(!$ret){
            $this->showmessage('暂不支持此银行卡!',['code'=>'024']);
        }

        return $ret;
    }
}